﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MiniX.API.Data;
using MiniX.Models;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Senparc.CO2NET.Extensions;
using MiniX.Utility.Helper;

namespace MiniX.API.Controllers
{
    /// <summary>
    /// 版本类，定义了CURD相关操作
    /// </summary>
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class VersionsController : BaseController<VersionsController>
    {
        /// <summary>
        /// VersionsController
        /// </summary>
        public VersionsController(MiniXApiContext context, IHttpContextAccessor httpContextAccessor) : base(context, httpContextAccessor)
        {

        }

        /// <summary>
        /// 根据ID获取版本详情
        /// </summary>
        /// <param name="id">版本ID</param>
        /// <returns>返回版本详情</returns>
        [HttpGet("/api/versions/{id}")]
        public async Task<IActionResult> GetById(int id)
        {
            try
            {
                log.Information("id=" + id);
                var result = await _context.mini_versions.Where(t => t.version_id == id).Select(item => (new
                {
                    item.version_id,
                    item.version_code,
                    item.version_name,
                    item.version_info,
                    item.force_update,
                    item.download_url,
                    item.version_type,
                    item.version_remark,
                    item.is_show,
                    item.created_at,
                    format_time = item.created_at.ToString("yyyy-MM-dd HH:mm")
                })).FirstOrDefaultAsync();
                if (result == null)
                {
                    return Json(new { code = 0, msg = "数据为空", count = 0 });
                }
                else
                {
                    return Json(new { code = 0, msg = "success", data = result });
                }
            }
            catch (Exception ex)
            {
                log.Fatal(ex, " -> Details");
                return Json(new { code = 1, errcode = (int)ENUMHelper.ExceptionType.Select, errmsg = ex.Message });
            }
        }
    }
}
